package com.jn.picasso.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.picasso.entity.BingImage;
import com.jn.picasso.views.ImageDisplayActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

public class BingImageAdapter extends RecyclerView.Adapter<BingImageAdapter.ViewHolder> {
    private Context context;
    private List<BingImage> images;

    public BingImageAdapter(Context context, List<BingImage> images) {
        this.context = context;
        this.images = images;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BingImage image = images.get(position);
        holder.title.setText(image.getTitle());
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, ImageDisplayActivity.class);
            intent.putExtra("imageUrl", image.getUrl());
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;

        ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(android.R.id.text1);
        }
    }
}